import { assign } from 'lodash'
import { fetchTableData, fetchTreeMap, fetchSdData } from '@/api/microGas'

const defaultState = () => {
  return {
    defaultDate: undefined,
    tableData: null,
    tableLoading: false,
    tableHint: '',
    tablePageParams: {
      pageNum: 1,
      pageSize: 20
    },
    stopTableKeyUp: false,
    treeMapList: [],
    currentMeterId: undefined,
    currentGasCls: undefined,
    sdList: [],
    sdLoading: false
  }
}

export default {
  namespaced: true,
  name: 'microGas',
  state: defaultState(),
  getters: {
    getTablePageParams: state => {
      return {
        ...state.tablePageParams
      }
    }
  },
  mutations: {
    REST_STATE(state) {
      state = assign(state, defaultState())
    },
    /**
     * desc: 设置表格数据
     * param {type}
     * return {type} void
     */
    SET_TABLE_DATA(state, tableData) {
      state.tableData = tableData
    },
    /**
     * desc: 设置表格加载状态
     * param {type}
     * return {type} void
     */
    SET_TABLE_LOADING(state, flag) {
      state.tableLoading = flag
    },
    /**
     * desc: 设置矩形树图数据
     * param {type}
     * return {type} void
     */
    SET_TREEMAP_DATA(state, data) {
      state.treeMapList = data
    },
    SET_CURRENT_METERID(state, meterId) {
      state.currentMeterId = meterId
    },
    /**
     * desc: 设置表格键盘操作标识符
     * param {type}
     * return {type} void
     */
    SET_STOP_TABLE_KEYUP(state, flag) {
      state.stopTableKeyUp = flag
    },
    /**
     * desc: 设置表格请求页数
     * param {type}
     * return {type} void
     */
    SET_TABLE_PAGE_NUM(state, num) {
      state.tablePageParams.pageNum = num
    },
    SET_CURRENT_GAS_CLs(state, type) {
      state.currentGasCls = type
    },
    SET_USER_QUERY_PARAMS(state, data) {
      state.userQueryParams = {
        ...state.userQueryParams,
        ...data
      }
    },
    SET_SD_LOADING(state, flag) {
      state.sdLoading = flag
    },
    SET_SD_LIST(state, list) {
      state.sdList = list
    }
  },
  actions: {
    clearStore(ctx) {
      ctx.commit('REST_STATE')
    },
    /**
     * desc: 获取表格数据
     * param {type}
     * return {type} void
     */
    async loadTableData(ctx, params) {
      ctx.commit('SET_TABLE_LOADING', true)
      ctx.commit('SET_CURRENT_METERID', undefined)
      const _tableData = await fetchTableData({
        ...ctx.state.tablePageParams,
        ...params
      })
      ctx.commit('SET_TABLE_LOADING', false)
      const { code, date = {} } = _tableData
      if (code === 200) {
        ctx.commit('SET_TABLE_DATA', date)
      }
    },
    /**
     * desc: 是否允许表格键盘操作
     * param {type}
     * return {type} void
     */
    changeStopTableKeyUp(ctx, flag) {
      ctx.commit('SET_STOP_TABLE_KEYUP', flag)
    },
    /**
     * desc: 修改表格页码
     * param {type}
     * return {type} void
     */
    changeTablePageNum(ctx, num) {
      ctx.commit('SET_TABLE_PAGE_NUM', num)
    },
    changeCurrentMeterId(ctx, meterId) {
      ctx.commit('SET_CURRENT_METERID', meterId)
    },
    /**
     * desc: 获取矩形树图
     * param {type}
     * return {type} void
     */
    async loadTreeMapData(ctx) {
      const { code, date } = await fetchTreeMap()
      if (code === 200) {
        ctx.commit('SET_TREEMAP_DATA', date)
      }
    },
    changeCurrentGasCls(ctx, type) {
      ctx.commit('SET_CURRENT_GAS_CLs', type)
    },
    changeUserQueryParams(ctx, data) {
      ctx.commit('SET_USER_QUERY_PARAMS', data)
    },
    async loadSdData(ctx, params) {
      ctx.commit('SET_SD_LOADING', true)
      const { code, date } = await fetchSdData(params)
      ctx.commit('SET_SD_LOADING', false)
      if (code === 200) {
        ctx.commit('SET_SD_LIST', date.microGasSplashes || [])
      }
    }
  }
}
